package net.wesc.ac.server;

public class Data {
	public static double[][] getData(String data){
		//确保data和model不为空
		if(data==null){
			return null;
		}
	    
		int index = 0;
		int rows = 0;
		int cols = 0;
		
		//获得数据行数,换行符为确定行数的标识符,并且要求在回车换行两个符号以前有实际数字存在
		while(data.charAt(index) != '\n'){
			if(data.charAt(index) == '\t'){
				cols++;
			}
			index++;
		}
		
		//System.out.println(cols);
		
		index = 0; //index重置
		while(index<data.length()){
			if(data.charAt(index) == '\n' && data.charAt(index-1) != '\n'){
				rows++;
			}
			index++;
		}//
		//System.out.println(rows+1);
		//System.out.println(cols+1);
	    double[][] expdata = new double[rows+1][cols+1];//最后一行无回车符存在,在上面确定行数时没有计数
	    
	    //实现输入实验数据的解析,生成数据的[rows+1][cols+1]维double型数组
	    index = 0;
	    int j = 0; //expdata的行坐标
	    int k = 0; //expdata的列坐标
	    StringBuffer tempString = new StringBuffer();
	    while(index<data.length()){
	    	if(data.charAt(index) != '\t' && data.charAt(index) != '\n'){
	    		tempString.append(data.charAt(index));
	    	}
	    	if(data.charAt(index) == '\t'){
		    	expdata[j][k] = new Double(new String(tempString)); 
	    		//System.out.print(new String(tempString) + "\t");
	    		tempString = new StringBuffer();
	    		k++;
	    	}
	    	if(data.charAt(index) == '\n'){
		    	expdata[j][k] = new Double(new String(tempString)); 
	    		//System.out.print(new String(tempString) + "\r\n");
	    		tempString = new StringBuffer();
	    		tempString = new StringBuffer();
	    		k=0;
	    		j++;
	    	}
	    	index ++;
	    }
    	expdata[j][k] = new Double(new String(tempString)); 
	    //System.out.print(new String(tempString) + "\r\n");
	    //(new Matrix(expdata)).print(10, 4);
		return expdata;
	}
}
